package com.wangtao.firstapp.adapters;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.wangtao.firstapp.R;
import com.wangtao.firstapp.activity.sife_secondactivity.OneSifeSecondActivity;
import com.wangtao.firstapp.activity.sife_secondactivity.ThreeSifeSecondActivity;
import com.wangtao.firstapp.activity.sife_secondactivity.TwoSifeSecondActivity;
import com.wangtao.firstapp.activity.sife_secondactivity.WebViewActivity;
import com.wangtao.firstapp.modles.OneSifeSecondLook;

import java.util.List;

/**
 * 二级页面-->小唐君有图页面
 * Created by lanouhn on 16/9/2.
 */
public class ACTSifeSecondLookAdapter extends RecyclerView.Adapter<ACTSifeSecondLookAdapter.LookViewHolder> {

    private Context context;
    private OneSifeSecondLook oneSifeSecondLook;


    public ACTSifeSecondLookAdapter(Context context, OneSifeSecondLook oneSifeSecondLook) {
        this.context = context;
        this.oneSifeSecondLook = oneSifeSecondLook;
    }

    @Override
    public LookViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(context).inflate(R.layout.actsifesecond_two_item_look, null);
        LookViewHolder holder = new LookViewHolder(view);
        return holder;
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public void onBindViewHolder(LookViewHolder holder, final int position) {

        final List<OneSifeSecondLook.DataBean.TopicBean> topicBean = oneSifeSecondLook.getData().getTopic();

        if (topicBean.size() > 0) {
            holder.linearLayout_view_look_one.setVisibility(View.VISIBLE);
            if (position == 0) {
                holder.linearLayout_text.setVisibility(View.VISIBLE);
            } else {
                int i = position - 1;

                holder.linearLayout_content.setVisibility(View.VISIBLE);
                holder.nameOne.setText(topicBean.get(i).getTitle());


                if (!topicBean.get(i).getPic().equals("")) {
                    Picasso.with(context).load(topicBean.get(i).getPic()).into(holder.photoOne);
                } else {
                    Picasso.with(context).load(topicBean.get(i).getPics().get(0).getUrl()).into(holder.photoOne);
                }


            }
        }
        //给“大家都在看”中的 三个条目设置点击事件，点击后分别进入不同的页面
        holder.linearLayout_content.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String item_ID = topicBean.get(position - 1).getId();

                //跳转至，作者原创
                if (topicBean.get(position - 1)
                        .getType().equals("2") && topicBean.get(position - 1)
                        .getType_id().equals("1")) {
                    Intent intent = new Intent(context, OneSifeSecondActivity.class);
                    intent.putExtra("item_ID", item_ID);
                    context.startActivity(intent);
                }

                //跳转至，小糖君无图
                if (topicBean.get(position - 1)
                        .getType().equals("5") && topicBean.get(position - 1)
                        .getType_id().equals("")) {
                    Intent intent = new Intent(context, ThreeSifeSecondActivity.class);
                    intent.putExtra("item_ID", item_ID);
                    context.startActivity(intent);
                }

                //跳转至，小糖君有图
                if (topicBean.get(position - 1)
                        .getType().equals("") && topicBean.get(position - 1)
                        .getType_id().equals("")) {
                    Intent intent = new Intent(context, TwoSifeSecondActivity.class);
                    intent.putExtra("item_ID", item_ID);
                    context.startActivity(intent);
                }

                //WebView
                if (topicBean.get(position - 1)
                        .getType().equals("2") && topicBean.get(position - 1)
                        .getType_id().equals("1")) {
                    Intent intent = new Intent(context, WebViewActivity.class);
                    intent.putExtra("item_ID", item_ID);
                    context.startActivity(intent);
                }

                //Toast.makeText(context, "进入所点击的页面", Toast.LENGTH_SHORT).show();
            }
        });

    }

    @Override
    public int getItemCount() {
        return oneSifeSecondLook.getData().getTopic().size() + 1;
    }

    public class LookViewHolder extends RecyclerView.ViewHolder {
        private ImageView photoOne;
        private TextView nameOne;
        private LinearLayout linearLayout_view_look_one, linearLayout_text, linearLayout_content;

        public LookViewHolder(View itemView) {
            super(itemView);
            linearLayout_view_look_one = (LinearLayout) itemView.findViewById(R.id.linearLayout_view_look_one);
            photoOne = (ImageView) itemView.findViewById(R.id.photoOne_actsifesecond_two_item_look);
            nameOne = (TextView) itemView.findViewById(R.id.nameOne_actsifesecond_two_item_look);
            linearLayout_content = (LinearLayout) itemView.findViewById(R.id.linearLayout_content);
            linearLayout_text = (LinearLayout) itemView.findViewById(R.id.linearLayout_text);
        }
    }
}
